<template>
  <div class="app-container home">
    <div>
      <el-form :inline="true" :model="formInline" class="">
        <el-form-item label="算例名称" prop="name">
          <el-input v-model="formInline.name" clearable placeholder="请输入" size="small"></el-input>
        </el-form-item>
        <el-form-item label="算例英文名" prop="algoCode">
          <el-input v-model="formInline.algoCode" clearable placeholder="请输入" size="small"></el-input>
        </el-form-item>
        <el-form-item label="算例类型" prop="algoType">
          <el-select v-model="formInline.algoType" >
            <el-option v-for="item in types" :label="item.name" :value="item.value" :key="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="算例描述" prop="description">
          <el-input v-model="formInline.description" clearable placeholder="请输入" size="small"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch" size="small">搜索</el-button>
          <el-button type="primary" plain @click="handleReset" size="small">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="btn">
      <el-button type="primary" size="small" @click="handleAdd">新增</el-button>
    </div>
    <el-table v-loading="loading" :data="tableData">
      <el-table-column label="序号" type="index" width="50" :index="indexMethod">
      </el-table-column>
      <el-table-column prop="algoName" label="算例名称"> </el-table-column>
      <el-table-column prop="algoCode" label="算例英文名"> </el-table-column>
      <el-table-column prop="algoDescribe" label="算例描述"> </el-table-column>
      <el-table-column prop="algoType" label="算例类型" />
      <el-table-column fixed="right" label="操作">
        <template slot-scope="scope">
          <el-button type="text" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button type="text" @click="editTemplate(scope.row)">编辑算例</el-button>
          <el-button type="text" @click="editSymbol(scope.row)">编辑符号</el-button>
          <el-button type="text" @click="keyRelation(scope.row)">字段映射</el-button>
          <el-button type="text" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination class="p20 fr" v-if="total > 0" :background="true" :current-page.sync="queryParams.pageNum"
      :page-size.sync="queryParams.pageSize" :layout="'total, sizes, prev, pager, next, jumper'"
      :page-sizes="[10, 30, 50, 100]" :total="total" @size-change="handleSizeChange"
      @current-change="handlepageNumChange" />
    <ConfigDialog :datas="configDatas" v-if="dialogVisible" :type="type" @on-close="handleDialogClose"
      @on-sure="onSearch" />
    <SymbolChoose :datas="configDatas" v-if="symbolVisible" @on-close="handleSymbolClose" @on-sure="onSearch" />
    <KeyRelationConfig :datas="configDatas" v-if="keyRelationVisible" @on-close="handleRelationClose" @on-sure="onSearch" />
  </div>
</template>

<script>
import ConfigDialog from "./component/ConfigDialog.vue";
import SymbolChoose from "./component/SymbolChoose.vue";
import KeyRelationConfig from "./component/KeyRelationConfig.vue";
import qs from "qs";
import { getAlgoList, deleteAlgo} from "@/api/computingConfig.js";
import { deleteTemplateInfo } from "@/api/business";
import store from "@/store";
export default {
  name: "computingConfig",
  components: { ConfigDialog, SymbolChoose ,KeyRelationConfig},
  data() {
    return {
      formInline: {
        name: "",
        algoCode: "",
        algoType: "",
        description: "",
      },
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      loading: false,
      total: 0,
      tableData: [],
      dialogVisible: false,
      configDatas: {},
      type: "add",
      computingTypeList: [],
      symbolVisible: false,
      keyRelationVisible:false,
      types: [{ name: "静强度算法", value: "STATIC_STRENGTH" }],
    };
  },
  methods: {
    indexMethod(index) {
      return (
        (this.queryParams.pageNum - 1) * this.queryParams.pageSize + index + 1
      );
    },
    async onSearch() {
      try {
        this.loading = true;
        const res = await getAlgoList({
          ...this.queryParams,
          ...this.formInline,
        });
        this.tableData = res.data.list;
        this.total = res.data.count;
      } catch (error) {
        console.debug(error);
      } finally {
        this.loading = false;
      }
    },
    handleReset() {
      this.formInline = {
        keyword: "",
        variableCode: "",
        variableNamespace: "",
        variableType: "",
      };
      this.onSearch();
    },
    handleSearch() {
      this.queryParams.pageNum = 1;
      this.onSearch();
    },
    handleSizeChange(val) {
      if (this.queryParams.pageSize * val > this.total) {
        this.queryParams.pageNum = 1;
      }
      this.onSearch();
    },
    handlepageNumChange() {
      this.onSearch();
    },
    handleAdd() {
      this.configDatas = {};
      this.type = "add";
      this.dialogVisible = true;
    },
    handleEdit(row) {
      this.configDatas = row;
      this.type = "edit";
      this.dialogVisible = true;
    },
    // 编辑模板
    editTemplate(row) {
      const query = qs.stringify({
        id: row.templateId,
        editContentType: "template",
      });
      window.open(`/editor/view?` + query, "_blank");
    },
    // 编辑符号
    editSymbol(row) {
      this.configDatas = row;
      this.symbolVisible = true;
    },
    handleDelete(row) {
      this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          deleteAlgo(row.id).then((res) => {
            this.$message({ type: "success", message: "删除成功!" });
            this.handleSearch();
            // 同时删除模板数据
            deleteTemplateInfo(row.templateId);
          });
        })
        .catch(() => {
          this.$message({ type: "info", message: "已取消删除" });
        });
    },
    handleDialogClose() {
      this.configDatas = {};
      this.dialogVisible = false;
    },
    handleSymbolClose() {
      this.configDatas = {};
      this.symbolVisible = false;
    },
    // 算例裕度汇总字段映射
    keyRelation(row){
      this.configDatas = row;
      this.keyRelationVisible = true;
    },
    handleRelationClose(){
      this.configDatas = {};
      this.keyRelationVisible = false;
    }
  },
  mounted() {
    this.handleSearch();
  },
};
</script>
<style scoped>
.btn {
  padding-bottom: 16px;
}
</style>